package com.wh.book2.service;

import com.wh.book2.enums.BookStatus;
import com.wh.book2.mapper.BookMapper;
import com.wh.book2.model.BookInfo;
import com.wh.book2.model.PageRequest;
import com.wh.book2.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {
    @Autowired
    BookMapper bookMapper;

    public int insertBook(BookInfo bookInfo) {
        return bookMapper.insertBook(bookInfo);
    }

    public PageResult<BookInfo> getBookListByPage(PageRequest pageRequest) {
        Integer count = bookMapper.getCount();
        List<BookInfo> bookListByPage =
                bookMapper.getBookListByPage(pageRequest.getOffSet(), pageRequest.getLimit());
        for (BookInfo book : bookListByPage) {
            book.setStatusCN(BookStatus.getDescByCode(book.getStatus()).getDesc());
        }
        return new PageResult(bookListByPage, count, pageRequest);
    }

    public Integer updateBookById(BookInfo bookInfo) {
        return bookMapper.updateBookById(bookInfo);
    }

    public BookInfo getBookById(Integer id) {
        return bookMapper.getBookById(id);
    }

    public Integer deleteBookById(Integer id) {
        BookInfo bookInfo = new BookInfo();
        bookInfo.setId(id);
        bookInfo.setStatus(0);
        return bookMapper.updateBookById(bookInfo);
    }

    public Integer batchDelete(List<Integer> ids) {
        return bookMapper.batchDelete(ids);
    }
}
